package com.chery.media.ui.music

import android.os.Bundle
import android.support.v4.media.MediaMetadataCompat
import android.support.v4.media.session.PlaybackStateCompat
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import androidx.core.view.isVisible
import com.chery.media.R
import com.chery.media.contacts.EventContacts
import com.chery.media.contacts.MusicType
import com.chery.media.databinding.ActivityMusicBinding
import com.chery.media.library.base.BaseVMActivity
import com.chery.media.library.base.NoViewModel
import com.chery.media.library.extend.clickNoRepeat
import com.chery.media.library.extend.loadPicture
import com.chery.media.library.extend.setNbOnItemClickListener
import com.chery.media.library.extend.statusBarTransparent
import com.chery.media.library.extend.toTime
import com.chery.media.library.extend.toast
import com.chery.media.music.BluetoothMusicTool
import com.jeremyliao.liveeventbus.LiveEventBus

class MusicActivity: BaseVMActivity<NoViewModel, ActivityMusicBinding>() {

    var totalTime = 0.0f

    var isJumpSetting = false

    private val musicTypeAdapter by lazy {
        MusicAdapter().apply {
            setOnItemClickListener { adapter, view, position ->
                setSelect(position)
                val type = when (position) {
                    0 -> MusicType.BLUETOOTH
                    1 -> MusicType.USB
                    else -> MusicType.HOT_LIST
                }
                if (type == musicViewModel.currentType.value) return@setOnItemClickListener
                if (musicViewModel.currentType.value == MusicType.BLUETOOTH && type != MusicType.BLUETOOTH) {
                    BluetoothMusicTool.unregisterCallback()
                    musicViewModel.mediaPlayerReset()
                } else if (musicViewModel.currentType.value != MusicType.BLUETOOTH && type == MusicType.BLUETOOTH){
                    musicViewModel.mediaPlayerReset()
                    BluetoothMusicTool.init()
                }
                isJumpSetting = false
                musicViewModel.setMusicType(type)
            }
        }
    }

    private val musicAdapter by lazy {
        MusicListAdapter().apply {
            setOnItemClickListener { adapter, view, position ->
                musicViewModel.playMusic(position)
                mBinding.infoLayout.isVisible = true
                mBinding.musicListRecycler.isVisible = false
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        statusBarTransparent()
        mBinding.musicType.adapter = musicTypeAdapter
        musicTypeAdapter.setList(musicViewModel.setTypes)
        mBinding.musicListRecycler.adapter = musicAdapter
    }

    override fun initData() {
        val type = intent.getIntExtra("type", 0)
        val musicId = intent.getStringExtra("musicId")
        if (musicId.isNullOrEmpty().not()) {
            musicViewModel.setMusicType(if (type == 0) MusicType.BLUETOOTH else if (type == 1) MusicType.USB else MusicType.HOT_LIST, musicId?:"")
        } else {
            val currentType = musicViewModel.currentType.value?: MusicType.BLUETOOTH
            musicViewModel.setMusicType(currentType)
        }
        musicTypeAdapter.setSelect(if (musicViewModel.currentType.value == MusicType.BLUETOOTH) 0 else if (musicViewModel.currentType.value == MusicType.USB) 1 else 2)
    }

    override fun onResume() {
        super.onResume()
        if (musicViewModel.currentType.value == MusicType.BLUETOOTH && isJumpSetting.not()) {
            if (musicViewModel.isBluetoothHeadsetConnected(this)) {
                BluetoothMusicTool.init()
            } else {
                musicViewModel.openBluetoothSettings(this)
                isJumpSetting = true
            }
        }
    }

    override fun initObserve() {
        musicViewModel.currentType.observe(this) {
//            if (it == MusicType.BLUETOOTH && isJumpSetting.not()) {
//                if (musicViewModel.isBluetoothHeadsetConnected(this)) {
//                    BluetoothMusicTool.init()
//                } else {
//                    musicViewModel.openBluetoothSettings(this)
//                    isJumpSetting = true
//                }
//            }
            setUI(it)
            mBinding.pause.setImageResource(R.drawable.svg_play)
            musicViewModel.playStatus.set(false)
        }
        musicViewModel.currentPlayMusic.observe(this) {
            if (it.isPlaying == true) {
                mBinding.pause.setImageResource(R.drawable.svg_play_pause)
                musicViewModel.playStatus.set(true)
            } else {
                mBinding.pause.setImageResource(R.drawable.svg_play)
                musicViewModel.playStatus.set(false)
            }
            mBinding.musicName.text = it.musicName
            mBinding.musicAuthor.text = it.musicAuthor
            totalTime = (it.totalTime?:"0").toFloat()
            mBinding.totalTime.text = (it.totalTime?:"0").toTime()
            mBinding.musicCover.loadPicture(it.musicCover, R.drawable.ic_default_cover)
            if (musicViewModel.currentType.value == MusicType.BLUETOOTH) {
                mBinding.musicBluetoothLyrics.text = ""
            } else {
                mBinding.musicBluetoothLyrics.text = ""
            }
            mBinding.playPosition.text = (it.playPosition?:"0").toTime()
            val seekBarProgress = ((it.playPosition?:"0").toFloat() / totalTime) * 100
            mBinding.musicSeeKBar.progress = seekBarProgress.toInt()
        }
        LiveEventBus.get<Boolean>(EventContacts.USB_STATUS).observe(this) {
            musicViewModel.getUsbMusicList()
        }
    }

    private fun setUI(type: MusicType){
        mBinding.musicName.text = ""
        mBinding.musicAuthor.text = ""
        mBinding.musicBluetoothLyrics.text = ""
        mBinding.playPosition.text = "00:00"
        mBinding.totalTime.text = "00:00"
        mBinding.musicSeeKBar.progress = 0
        when (type) {
            MusicType.BLUETOOTH -> {
                mBinding.musicList.isVisible = false
                mBinding.musicListRecycler.isVisible = false
                mBinding.infoLayout.isVisible = true
                mBinding.bottomOperate.isVisible = true
                mBinding.usbEmpty.isVisible = false
            }
            MusicType.USB -> {
                mBinding.musicList.isVisible = true
                mBinding.musicListRecycler.isVisible = false
                mBinding.infoLayout.isVisible = true
                mBinding.bottomOperate.isVisible = true
                mBinding.usbEmpty.isVisible = false
            }
            MusicType.HOT_LIST -> {
                mBinding.musicList.isVisible = true
                mBinding.musicListRecycler.isVisible = false
                mBinding.infoLayout.isVisible = true
                mBinding.bottomOperate.isVisible = true
                mBinding.usbEmpty.isVisible = false
            }
        }
    }

    override fun initListener(){
        mBinding.previousSong.clickNoRepeat {
            musicViewModel.playPrevious()
        }
        mBinding.pause.clickNoRepeat {
            if (musicViewModel.playStatus.get()) {
                musicViewModel.playPause()
            } else {
                musicViewModel.playMusic(0)
            }
        }
        mBinding.nextSong.clickNoRepeat {
            musicViewModel.playNext()
        }
        mBinding.musicList.clickNoRepeat {
            if (mBinding.musicListRecycler.isVisible) {
                mBinding.infoLayout.isVisible = true
                mBinding.musicListRecycler.isVisible = false
            } else {
                musicAdapter.setList(musicViewModel.musicService?.musicList)
                mBinding.infoLayout.isVisible = false
                mBinding.musicListRecycler.isVisible = true
            }
        }
        mBinding.musicSeeKBar.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
            }

        })
    }

}